﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace LenderClearingHouse
{
  public partial class EditUserControl : UserControlBase
  {
    protected override void OnInit(EventArgs e)
    {
      base.OnInit(e);
      this.DataBinding += new EventHandler(EditUserControl_DataBinding);
    }

    protected override void OnLoad(EventArgs e)
    {
      base.OnLoad(e);

      tbPassword.Attributes.Add("value", tbPassword.Text);
      tbConfirmPassword.Attributes.Add("value", tbConfirmPassword.Text);
    }

    object _dataItem = null;

    public object DataItem
    {
      get
      {
        return this._dataItem;
      }
      set
      {
        this._dataItem = value;
      }
    }

    public UserRole Role
    {
      get
      {
          if (rbSuperAdmin.Checked && rbSuperAdmin.Visible) return UserRole.SuperAdmin;
          else if (rbInvestorAdmin.Checked && rbInvestorAdmin.Visible) return UserRole.InvestorAdmin;
          else if (rbInvestor.Checked && rbInvestor.Visible) return UserRole.InvestorUser;
          else if (rbUser.Checked && rbUser.Visible) return UserRole.User;
          else if (rbEscrowUser.Checked && rbEscrowUser.Visible) return UserRole.EscrowUser;
          else if (rbProjectManager.Checked && rbProjectManager.Visible) return UserRole.ProjectManager;
          else throw new NotImplementedException();
      }
    }

    void EditUserControl_DataBinding(object sender, EventArgs e)
    {
      rbSuperAdmin.Visible = CurrentUser.UserRole == UserRole.SuperAdmin;
      rbInvestorAdmin.Visible = CurrentUser.UserRole == UserRole.SuperAdmin;
      rbUser.Visible = CurrentUser.UserRole == UserRole.SuperAdmin;
      rbEscrowUser.Visible = CurrentUser.UserRole == UserRole.SuperAdmin;
      rbProjectManager.Visible = CurrentUser.UserRole == UserRole.SuperAdmin;

      if (CurrentUser.UserRole == UserRole.InvestorAdmin)
      {
        rbInvestor.Checked = true;

        if (CurrentUser.SubRole == UserSubRole.LVReo)
        {
          ddlInvestorRole.Items.Remove(ddlInvestorRole.Items.FindByValue("S"));
        }
        else if (CurrentUser.SubRole == UserSubRole.SDReo)
        {
          ddlInvestorRole.Items.Remove(ddlInvestorRole.Items.FindByValue("L"));
        }
      }


      if (DataItem is GridInsertionObject)
      {
        cbActive.Checked = true;
        
        ddlLvreoRole.SelectedValue = "NA";
        ddlSdreoRole.SelectedValue = "NA";
        ddlMasterlist.SelectedValue = "NA";
      }
      else
      {
        cbActive.Checked = (bool)DataBinder.Eval(DataItem, "Is_Active");
        cbVIP.Checked = (bool)DataBinder.Eval(DataItem, "IsVIP");

        ddlLvreoRole.SelectedValue = (string)DataBinder.Eval(DataItem, "LvreoRole");
        ddlSdreoRole.SelectedValue = (string)DataBinder.Eval(DataItem, "SdreoRole");
        ddlMasterlist.SelectedValue = (string)DataBinder.Eval(DataItem, "MasterlistRole");

        string userRole = (string)DataBinder.Eval(DataItem, "Role");
        string subRole = (string)DataBinder.Eval(DataItem, "SubRole");
        if(userRole == "IA")
        {
          rbInvestorAdmin.Checked = true;
          ddlInvestorAdminRole.SelectedValue = subRole;
        }
        else if(userRole == "IU")
        {
            rbInvestor.Checked = true;
            ddlInvestorRole.SelectedValue = subRole;
        }
        else if (userRole == "US")
        {
          rbUser.Checked = true;
        }
        else if (userRole == "EU")
        {
            rbEscrowUser.Checked = true;
        }
        else if (userRole == "PM")
        {
          rbProjectManager.Checked = true;
        }   
        else if (userRole == "SA")
        {
          rbSuperAdmin.Checked = true;
        }
      }
      
      cbIsAdmin_CheckedChanged(null, null);
      rbSuperAdmin_CheckedChanged(null, null);

      if (!(DataItem is GridInsertionObject))
      {
        try
        {
          int createdByUser = (int)DataBinder.Eval(DataItem, "CreatedByUser");
          if (ddlInvestorAdmin.Items.FindByValue(createdByUser.ToString()) != null)
          {
            ddlInvestorAdmin.SelectedValue = createdByUser.ToString();
          }
        }
        catch { }
      }
    }

    protected void cbIsAdmin_CheckedChanged(object sender, EventArgs e)
    {
      //if (!cbActive.Checked)
      //{
      //  tdLvreo.Style[HtmlTextWriterStyle.Display] = "none";
      //  tdSdreo.Style[HtmlTextWriterStyle.Display] = "none";
      //  tdMasterlist.Style[HtmlTextWriterStyle.Display] = "none";

      //  ddlLvreoRole.SelectedValue = "NA";
      //  ddlSdreoRole.SelectedValue = "NA";
      //  ddlMasterlist.SelectedValue = "NA";
      //}
      //else
      //{
      //  if (cbIsAdmin.Checked)
      //  {
      //    tdLvreo.Style[HtmlTextWriterStyle.Display] = "none";
      //    tdSdreo.Style[HtmlTextWriterStyle.Display] = "none";
      //    tdMasterlist.Style[HtmlTextWriterStyle.Display] = "none";

      //    ddlLvreoRole.SelectedValue = "RW";
      //    ddlSdreoRole.SelectedValue = "RW";
      //    ddlMasterlist.SelectedValue = "RW";
      //  }
      //  else
      //  {
      //    tdLvreo.Style[HtmlTextWriterStyle.Display] = "";
      //    tdSdreo.Style[HtmlTextWriterStyle.Display] = "";
      //    tdMasterlist.Style[HtmlTextWriterStyle.Display] = "";
      //  }
      //}
    }

    protected void rbSuperAdmin_CheckedChanged(object sender, EventArgs e)
    {
      ddlInvestorAdminRole.Visible = rbInvestorAdmin.Checked;
      
      ddlInvestorRole.Visible = rbInvestor.Checked && CurrentUser.UserRole != UserRole.InvestorAdmin;
      ddlInvestorAdmin.Visible = ddlInvestorRole.Visible;
      ddlInvestorRole_SelectedIndexChanged(null, null);
      lbAdminText.Visible = ddlInvestorAdmin.Visible;
      cbVIP.Visible = ddlInvestorRole.Visible;
      
      tblUserRoles.Visible = rbUser.Checked;
    }

    protected void ddlInvestorRole_SelectedIndexChanged(object sender, EventArgs e)
    {
      ddlInvestorAdmin.Items.Clear();

      DataAccess da = new DataAccess();
      DataTable tbl = da.ExecuteSQL("getInvestorAdmins '" + ddlInvestorRole.SelectedValue + "'");
      foreach (DataRow dr in tbl.Rows)
      {
        ddlInvestorAdmin.Items.Add(new ListItem(dr["InvestorName"].ToString(), dr["UserId"].ToString()));
      }
    }
  }
}